package com.yuerlive.rainbow.module.mine.presenter;


import com.yuerlive.rainbow.api.MsgApi;
import com.yuerlive.rainbow.api.MsgService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.MyMsgData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 2017/5/31.
 */

public class MyMessagePresenter extends BasePresenterImpl implements MyMessageContract.Presenter {
    private MyMessageContract.View view;
    private MsgService service;
    private int page = 1;
    private int pageSize = 10;

    public MyMessagePresenter(MyMessageContract.View view) {
        this.view = view;
        view.setPresenter(this);
        service = MsgApi.getInstance().msgService;
    }

    @Override
    public void getMyMessage(boolean isLoadMore) {
        if (isLoadMore) {
            page++;
        } else {
            page = 1;
        }
        view.showLoading();
        User user = UserProxy.getUser();
        Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));
        params.put("pageSize", String.valueOf(pageSize));
        L.e("MyMessagePresenter", "getMyMessage");
        service.getMyMsg(params).compose(RxHelper.toUI()).subscribe(new RxObserver<BaseData<MyMsgData>>(this) {
            @Override
            protected void onSuccess(BaseData<MyMsgData> myMsgInfoBaseData) {
                L.e("MyMessagePresenter", "onSuccess");
                view.hideLoading();
                view.showMessageData(myMsgInfoBaseData.getObject().getList(), page);
            }

            @Override
            protected void onFail(String message) {
                L.e("MyMessagePresenter", "onFail");
            }
        });
    }
}
